package com.cmccgz.udacs_landlord_webapp.utils;


import android.app.Activity;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cmccgz.udacs_landlord_webapp.R;

public class ViewHelper {
    private Object link;
    private TextView tv_title;
    private TextView tv_back;
    private TextView tv_right;
    private ImageView iv_left;
    private ImageView iv_right;
    private ImageView iv_left_point;

    private ViewHelper( Object link){
        this.link=link;
        initView();
    }

    public static ViewHelper instance(Object link){
        if (link==null||!(link instanceof Activity||link instanceof Fragment)){
            return null;
        }
        return new ViewHelper(link);
    }

    private void initView(){
        tv_title= (TextView) findViewById(R.id.tv_title);
        tv_back= (TextView) findViewById(R.id.tv_back);
        tv_right= (TextView) findViewById(R.id.tv_right);
        iv_left= (ImageView) findViewById(R.id.iv_left);
        iv_right= (ImageView) findViewById(R.id.iv_right);
        iv_left_point = ((ImageView) findViewById(R.id.iv_left_point));
        if (link instanceof View.OnClickListener) {
            setTitleOnClicklistener((View.OnClickListener) link);
        }
    }
    private View findViewById(@IdRes int id){
        if (link instanceof Activity) {
            return ((Activity) link).findViewById(id);
        }else  if (link instanceof Fragment){
            return ((Fragment) link).getView().findViewById(id);
        }
        return null;
    }
    public void setTitle(String title){
        if (tv_title!=null) {
            tv_title.setText(title);
        }
    }
    public void setTitle(@StringRes int strId){
        if (tv_title!=null) {
            tv_title.setText(strId);
        }
    }
    public void setBackText(String backText){
        if (tv_back!=null) {
            tv_back.setText(backText);
        }
    }
    public void setBackText(@StringRes int strId){
        if (tv_back!=null) {
            tv_back.setText(strId);
        }
    }
    public void setRightText(String rightText){
        if (tv_right!=null) {
            tv_right.setText(rightText);
        }
    }
    public void setRightText(@StringRes int strId){
        if (tv_right!=null) {
            tv_right.setText(strId);
        }
    }
    public void setImageLeft(@DrawableRes int drawableRes){
        if (iv_left!=null) {
            iv_left.setImageResource(drawableRes);
        }
    }
    public void setImageRight(@DrawableRes int drawableRes){
        if (iv_right!=null) {
            iv_right.setImageResource(drawableRes);
        }
    }
    public void setImageLeftVisable(boolean isVisable){
        if (iv_left==null) {
            return;
        }
        iv_left.setVisibility(isVisable?View.VISIBLE:View.INVISIBLE);
    }
    public void setPointLeftVisable(boolean isVisable){
        if (iv_left_point==null) {
            return;
        }
        iv_left_point.setVisibility(isVisable?View.VISIBLE:View.INVISIBLE);
    }
    public void setImageRightVisable(boolean isVisable){
        if (iv_right==null) {
            return;
        }
        iv_right.setVisibility(isVisable?View.VISIBLE:View.INVISIBLE);
    }
    public void setTextRightVisable(boolean isVisable){
        if (tv_right==null) {
            return;
        }
        tv_right.setVisibility(isVisable?View.VISIBLE:View.INVISIBLE);
    }
    public void setBackTextVisable(boolean isVisable){
        if (tv_back==null) {
            return;
        }
        tv_back.setVisibility(isVisable?View.VISIBLE:View.INVISIBLE);
    }
    public void setAllVisable(boolean isVisable){
        setBackTextVisable(isVisable);
        setImageLeftVisable(isVisable);
        setImageRightVisable(isVisable);
        setTextRightVisable(isVisable);
    }
    public void setTitleOnClicklistener(View.OnClickListener listener){
        if (tv_back!=null) {
            tv_back.setOnClickListener(listener);
        }
        if (iv_left!=null) {
            iv_left.setOnClickListener(listener);
        }
        if (iv_right!=null) {
            iv_right.setOnClickListener(listener);
        }
        if (tv_right!=null) {
            tv_right.setOnClickListener(listener);
        }
    }

    public TextView getTv_title() {
        return tv_title;
    }

    public TextView getTv_back() {
        return tv_back;
    }

    public TextView getTv_right() {
        return tv_right;
    }

    public ImageView getIv_left() {
        return iv_left;
    }

    public ImageView getIv_right() {
        return iv_right;
    }
}
